<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-3-13 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'include/lib/cache.func.php';
require ROOT_PATH.'include/lib/template.func.php';
require ROOT_PATH.'include/sql/sql_functions.php';
require_once ROOT_PATH.'include/function/form.func.php';

class masterObj
{
	var $request = array();
	
	var $mod = '';
	var $act = '';
	var $user = array();
	var $session = array();
	
	var $_is_messager = false;

	function __construct($request)
	{
		load_setting('config_global');
		
		$this->request = $request;
		$this->mod = isset($request['m']) ? $request['m'] : '';
		$this->act = isset($request['a']) ? $request['a'] : '';
		
		$this->request['limit'] = 20;
		$this->request['offset'] = 0;
		
		if($this->mod != 'login')
		{
			$this->_checkUser();
		}
		
		// if not admin, can manage one business
		if(($this->act == 'add' || $this->act == 'do_add') && $this->user['group_id'] > 1 && check_array($this->user['item_list']))
		{
			$act = $this->act;
			$this->act = '';
			
			
			if($this->user['group_id'] == '3' || $this->user['group_id'] == '4')
			{
				if($this->mod == 'hostel')
				{
					$this->act = $act;
					foreach($this->user['item_list'] as $item)
					{
						if($item['type'] == 'ho')
						{
							$this->act = '';
							break;
						}
					}
				}
			}
			
		}
	}

	function showBody($body)
	{
		echo $body;
		
		if($this->mod != 'index' || isset($this->act))$this->showFooter();
	}
	
	function showHeader($title, $additional_file_list=array(), $additional_str="", $sub_menu_list=array(), $header_menu_list=array())
	{
		include template('admin/header');
	}
	
	function showFooter()
	{
		include template('admin/footer');
	}
	
	function copyRight()
	{
		$year = date('Y');
		echo '<div style="clear:both;text-align:center;margin:5px auto;">Copyright&copy; '.$year.', nikm</div>';
	}
	
	function actionName()
	{
		$action_name = isset($this->request['action_name']) ? trim($this->request['action_name']) : '';
		
		if(!empty($action_name))return $action_name;
		
		include  ROOT_PATH.'config/menu_list_admin.php';
		foreach($menu_list as $_menu_list)
		{
			if(!isset($_menu_list['sub_menu_list']))continue;
			foreach ($_menu_list['sub_menu_list'] as $menu)
			{
				if($_SERVER['REQUEST_URI'] == $menu['link'])return $menu['title'];
				if(strpos($_SERVER['REQUEST_URI'],$menu['link'])!==false)
				{
					$action_name = $menu['title'];
				}
			}
		}
		return $action_name;
	}
	
	function messager($message, $redirect_to='', $time=2, $return_msg=false, $js=null)
	{
		$this->_is_messager = true;
		
		$to_title = ($redirect_to === '' || $redirect_to == -1) ? "返回上一页" : "跳转到指定页面";
		
		if($redirect_to === null)
		{
			$return_msg = ($return_msg === false) ? "&nbsp;" : $return_msg;
		}
		else
		{
			$redirect_to = ($redirect_to !== '') ? $redirect_to : ($from_referer = referer());
			
			if(is_numeric($redirect_to) !== false && $redirect_to !== 0)
			{
				if($time !== null)
				{
					$url_redirect = "<script type=\"text/javascript\">\r\n";
					$url_redirect .= sprintf("window.setTimeout(\"history.go(%s)\",%s);\r\n", $redirect_to, $time*1000);
					$url_redirect .= "</script>\r\n";
				}
				
				$redirect_to = "javascript:history.go({$redirect_to})";
			}
			else
			{
				if($message === null)
				{
					$redirect_to = rawurldecode(stripslashes(($redirect_to)));
					@header("Location: $redirect_to"); #HEADER跳转
				}
				if($time !== null)
				{
					$url_redirect = $redirect_to ? '<meta http-equiv="refresh" content="'.$time.'; URL='.$redirect_to.'">' : null;
				}
			}
		}
		
		$title = "消息提示:".(is_array($message) ? implode(',', $message) : $message);

		$title = strip_tags($title);
		if($js != '')$js = "<script type=\"text/javascript\">{$js}</script>";

		ob_start();
		$this->showHeader($title);
		include_once template('messager');
		$body = ob_get_clean();

		$this->showBody($body);
		exit;
	}
	
	/**
	 * 
	 * Check if user login & has permission
	 */
	private function _checkUser()
	{
		// no login
		if(!isset($_SESSION['user']) || $_SESSION['user']['id'] < 1)
		{
			$title = "用户登录";
			$action = "do_login";
			
			include template("login");
			exit();
		}
		
		$this->user = $_SESSION['user'];
		
		if(!isset($this->user['group_id']) || $this->user['group_id'] < 1)
		{
			$this->messager("您无权访问");
		}
		
		return true;
	}
	
	protected function createPassword($date, $password)
	{
		$validate_pwd = password_hash($date, $password);
		
		return $validate_pwd;
	}
}